package com.ruoyi.web.controller.system;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.KingTemporary.FloatNumber;
import com.ruoyi.system.domain.KingTemporary.KingCurrency;
import com.ruoyi.system.domain.KingTemporary.StkMisc;
import com.ruoyi.system.service.IKingBdCangService;
import com.ruoyi.system.service.IKingBdOrganizationService;
import com.ruoyi.system.service.King.IKingCanService;
import com.ruoyi.web.until.DesUtil;
import com.ruoyi.web.until.InvokeHelper;
import com.ruoyi.web.until.ResultUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/king/login")
public class KingLoginController extends BaseController
{
//    @Autowired
//    private ISysUserService userService;
    //账套
    private static String zhangtao = "60a22dac73e06a";
    //字符编码
    private static Integer codes = 2052;

    @Autowired
    private IKingCanService iKingCanService;
    @Autowired
    private IKingBdOrganizationService iKingBdOrganizationService;
    @Autowired
    private IKingBdCangService iKingBdCangService;
    @Autowired
    private RedisCache redisCache;

    /**
     * 用户登录
     */
    @CrossOrigin
    @PostMapping("/login")
    @SuppressWarnings("unchecked")
    public ResultUtil getInstance(@RequestBody KingUser kingUser) throws Exception {
        Map<String, Object> hashMap = new HashMap();
        if(StringUtils.isNotEmpty(kingUser.getUsername())&& StringUtils.isNotEmpty(kingUser.getPassword())){
            hashMap = InvokeHelper.Login2(zhangtao,kingUser.getUsername(),kingUser.getPassword(),codes);
            return ResultUtil.success(JSON.toJSON(hashMap));
        }else {
            return ResultUtil.fail("700","参数为空");
        }

    }

    @CrossOrigin
    @PostMapping("/SaveOutorder")
    public ResultUtil SaveOutorder(@RequestBody JSONObject jsonObject) throws Exception {
       if(jsonObject.containsKey("token")){
           if(jsonObject.getString("token").length()>0) {
               String ssd = DesUtil.decrypt(jsonObject.getString("token"));
               String jsonString = iKingCanService.getSubParam(jsonObject);
               //InvokeHelper.Save("STK_MISCELLANEOUS", AssembleUtil.biao(), DesUtil.decrypt(jsonObject.getString("token")));
               JSONObject jsonObject1 = InvokeHelper.Save("STK_MISCELLANEOUS", jsonString, InvokeHelper.getUserCook((jsonObject.getString("userCode"))));
               String flag = jsonObject1.getJSONObject("Result").getJSONObject("ResponseStatus").getString("IsSuccess");
               if(flag.equals("true")){
                   return ResultUtil.success("");
               }else{
                   JSONObject json = (JSONObject) JSON.toJSON(jsonObject1.getJSONObject("Result").getJSONObject("ResponseStatus").getJSONArray("Errors").get(0));
                   String msg1 = json.getString("Message");
                   return ResultUtil.fail("900",msg1);
               }
           }else {
               return ResultUtil.fail("800","会话信息已丢失，请重新登录");
           }
       }else {
           return ResultUtil.fail("700","参数为空");
       }
    }

    //根据用户内码查询其它入库单list
    @CrossOrigin
    @PostMapping("/getUserQtrkList")
    public String getUserQtrkList(@RequestBody String fId) throws Exception {
        List<StkMisc> list = iKingCanService.userIdToStkmiscList(fId);
        List<KingZjRk> list1 = iKingCanService.getUserStk(list);
        List<Map<String, Object>> jsonArray = iKingCanService.getQitaRuArry(list1);
        return JSONObject.toJSONString(jsonArray);
    }

    //获取组织机构list
    @CrossOrigin
    @PostMapping("/getOrgan")
    public String getOrgan(){
        if(redisCache.getCacheList("Organ").size()>0){
            JSONArray array= JSONArray.parseArray(JSON.toJSONString(redisCache.getCacheList("Organ")));
            return array.toString();
        }else{
            List<Map<String, String>> list = iKingBdOrganizationService.getALL();
            redisCache.setCacheList("Organ",list);
            JSONArray array= JSONArray.parseArray(JSON.toJSONString(list));
            return array.toString();
        }


    }

    //根据组织内码获取入库类型
    @CrossOrigin
    @PostMapping("/getRukuLei")
    public String getRukuLei(@RequestBody JSONObject jsonObject){
        if(jsonObject.containsKey("cId")){
            if(jsonObject.getString("cId").length()>0){
                List<Map<String, String>> list = iKingCanService.getRukuLei(jsonObject.getString("cId"));
                JSONArray array= JSONArray.parseArray(JSON.toJSONString(list));
                return array.toString();
            }else {
                return "";
            }
        }else {
            return "";
        }

    }

    //根据组织内码获取物料
    @CrossOrigin
    @PostMapping("/getWuList")
    public String getWuList(@RequestBody JSONObject jsonObject){
        if(jsonObject.containsKey("fId")){
            return iKingCanService.fIdToWuLiao(jsonObject.getString("fId"));
        }else {
            return "";
        }
    }

    //根据组织内码获取物料
    @CrossOrigin
    @PostMapping("/getWuList2")
    public List<Map<String, String>> getWuList2(@RequestBody JSONObject jsonObject){
        if(jsonObject.containsKey("fId")){
            return iKingCanService.getWuliao(jsonObject.getString("fId"));
        }else {
            return new ArrayList<>();
        }
    }

    //根据组织code获取库存物料
    @CrossOrigin
    @PostMapping("/getWuListChu")
    public String getWuListChu(@RequestBody JSONObject jsonObject){
        if(jsonObject.containsKey("fId")){
            return iKingCanService.fIdToWuLiaoKuCun(iKingBdOrganizationService.fCodeToId(jsonObject.getString("fId")));
        }else {
            return "";
        }
    }

    //根据组织code获取库存物料
    @CrossOrigin
    @PostMapping("/getWuListChu2")
    @SuppressWarnings("unchecked")
    public List<Map<String, String>> getWuListChu2(@RequestBody JSONObject jsonObject){
        if(jsonObject.containsKey("fId")){
            return iKingCanService.getKuCunWuliao(iKingBdOrganizationService.fCodeToId(jsonObject.getString("fId")));
        }else {
            return new ArrayList<>();
        }
    }

    //获取种苗类型
    @CrossOrigin
    @PostMapping("/getZhongMiaoList")
    public String getZhongMiaoList(){
        List<Map<String, String>> list = iKingCanService.getZhongList();
        JSONArray array= JSONArray.parseArray(JSON.toJSONString(list));
        return array.toString();
    }

    //获取库存状态
    @CrossOrigin
    @PostMapping("/getKuStateList")
    public String getKuStateList(){
        List<Map<String, String>> list =iKingCanService.getKuCuState();
        JSONArray array= JSONArray.parseArray(JSON.toJSONString(list));
        return array.toString();
    }

    //根据物料编码查询是否启用产品形态
    @CrossOrigin
    @PostMapping("/getWuliaoCp")
    public String getWuliaoCp(@RequestBody String fCode) {
        return iKingCanService.selectWuliaoCp(fCode);
    }


    //提交其它入库单
    @CrossOrigin
    @PostMapping("/submitSTk")
    public ResultUtil submitSTk(@RequestBody JSONObject jsonObject) throws Exception {
        if(jsonObject.containsKey("token")){
            if(jsonObject.getString("token").length()>0) {
                String jsonString = iKingCanService.getSubmitParm(jsonObject.getString("fCode"));
                JSONObject jsonObject1 = InvokeHelper.Submit("STK_MISCELLANEOUS", jsonString,InvokeHelper.getUserCook((jsonObject.getString("userCode"))));
                return getResult(jsonObject1);
            }else {
                return ResultUtil.fail("800","会话信息已丢失，请重新登录");
            }
        }else {
            return ResultUtil.fail("700","参数为空");
        }
    }

    //审核其它入库单
    @CrossOrigin
    @PostMapping("/examSTK")
    public ResultUtil examSTK(@RequestBody JSONObject jsonObject) throws Exception {
        if(jsonObject.containsKey("token")){
            if(jsonObject.getString("token").length()>0) {
                String jsonString = iKingCanService.getSubmitParm(jsonObject.getString("fCode"));
                JSONObject jsonObject1 = InvokeHelper.Audit("STK_MISCELLANEOUS", jsonString, InvokeHelper.getUserCook((jsonObject.getString("userCode"))));
                return getResult(jsonObject1);
            }else {
                return ResultUtil.fail("800","会话信息已丢失，请重新登录");
            }
        }else {
            return ResultUtil.fail("700","参数为空");
        }
    }

    //反审核其它入库单
    @CrossOrigin
    @PostMapping("/unexamSTK")
    public ResultUtil unexamSTK(@RequestBody JSONObject jsonObject) throws Exception {
        if(jsonObject.containsKey("token")){
            if(jsonObject.getString("token").length()>0) {
                String jsonString = iKingCanService.getSubmitParm(jsonObject.getString("fCode"));
                JSONObject jsonObject1 = InvokeHelper.UnAudit("STK_MISCELLANEOUS", jsonString, InvokeHelper.getUserCook((jsonObject.getString("userCode"))));
                return getResult(jsonObject1);
            }else {
                return ResultUtil.fail("800","会话信息已丢失，请重新登录");
            }
        }else {
            return ResultUtil.fail("700","参数为空");
        }
    }

    public static ResultUtil getResult(JSONObject jsonObject1){
        String flag = jsonObject1.getJSONObject("Result").getJSONObject("ResponseStatus").getString("IsSuccess");
        if(flag.equals("true")){
            return ResultUtil.success("");
        }else{
            JSONObject json = (JSONObject) JSON.toJSON(jsonObject1.getJSONObject("Result").getJSONObject("ResponseStatus").getJSONArray("Errors").get(0));
            String msg1 = json.getString("Message");
            return ResultUtil.fail("900",msg1);
        }
    }

    //获取机构部门list
    @CrossOrigin
    @PostMapping("/getOrganDepartList")
    public String getOrganDepartList(){
        return  iKingBdOrganizationService.getOrganDepartList();
    }

    //获取机构领用明细
    @CrossOrigin
    @PostMapping("/getCellDetail")
    public String getCellDetail(@RequestBody String fId){
        return JSONObject.toJSONString(iKingCanService.getCellDetail(iKingBdOrganizationService.fCodeToId(fId)));
    }

    //根据用户内码查询其它出库单list
    @CrossOrigin
    @PostMapping("/getUserQtckList")
    public String getUserQtckList(@RequestBody String fId) throws Exception {
        List<StkMisc> list = iKingCanService.useridToSTKchuList(fId);
        List<KingZjRk> list1 = iKingCanService.getUserStkChu(list);
        List<Map<String, Object>> jsonArray = iKingCanService.getQitaRuArry(list1);
        return JSONObject.toJSONString(jsonArray);
    }

    //提交其它出库单
    @CrossOrigin
    @PostMapping("/submitSTkChu")
    public ResultUtil submitSTkChu(@RequestBody JSONObject jsonObject) throws Exception {
        if(jsonObject.containsKey("token")){
            if(jsonObject.getString("token").length()>0) {
                String jsonString = iKingCanService.getSubmitParm(jsonObject.getString("fCode"));
                JSONObject jsonObject1 = InvokeHelper.Submit("STK_MisDelivery", jsonString,InvokeHelper.getUserCook((jsonObject.getString("userCode"))));
                return getResult(jsonObject1);
            }else {
                return ResultUtil.fail("800","会话信息已丢失，请重新登录");
            }
        }else {
            return ResultUtil.fail("700","参数为空");
        }
    }

    //审核其它出库单
    @CrossOrigin
    @PostMapping("/examSTKChu")
    public ResultUtil examSTKChu(@RequestBody JSONObject jsonObject) throws Exception {
        if(jsonObject.containsKey("token")){
            if(jsonObject.getString("token").length()>0) {
                String jsonString = iKingCanService.getSubmitParm(jsonObject.getString("fCode"));
                JSONObject jsonObject1 = InvokeHelper.Audit("STK_MisDelivery", jsonString,InvokeHelper.getUserCook((jsonObject.getString("userCode"))));
                return getResult(jsonObject1);
            }else {
                return ResultUtil.fail("800","会话信息已丢失，请重新登录");
            }
        }else {
            return ResultUtil.fail("700","参数为空");
        }
    }

    //反审核其它出库单
    @CrossOrigin
    @PostMapping("/unexamSTKChu")
    public ResultUtil unexamSTKChu(@RequestBody JSONObject jsonObject) throws Exception {
        if(jsonObject.containsKey("token")){
            if(jsonObject.getString("token").length()>0) {
                String jsonString = iKingCanService.getSubmitParm(jsonObject.getString("fCode"));
                JSONObject jsonObject1 = InvokeHelper.UnAudit("STK_MisDelivery", jsonString, InvokeHelper.getUserCook((jsonObject.getString("userCode"))));
                return getResult(jsonObject1);
            }else {
                return ResultUtil.fail("800","会话信息已丢失，请重新登录");
            }
        }else {
            return ResultUtil.fail("700","参数为空");
        }
    }

    //根据机构内码和物料code查询仓库
    @CrossOrigin
    @PostMapping("/getKuCunCanList")
    public String getKuCunCanList(@RequestBody JSONObject jsonObject){
        if(jsonObject.containsKey("fId") && jsonObject.containsKey("fCode")){
            if(jsonObject.getString("fId").length()>0 && jsonObject.getString("fCode").length()>0){
                return iKingCanService.getKuCunCanList(iKingBdOrganizationService.fCodeToId(jsonObject.getString("fId")),jsonObject.getString("fCode"));
            }else {
                return "";
            }
        }else {
            return "";
        }
    }

    //根据机构内码和物料code,仓库信息查询批号
    @CrossOrigin
    @PostMapping("/getFloatNumber")
    public String getFloatNumber(@RequestBody FloatNumber floatNumber){
        if(floatNumber!=null){
            return iKingCanService.getFloatNumber(floatNumber);
        }else {
            return "";
        }
    }

    //根据机构内码和物料code,仓库信息查询批号
    @CrossOrigin
    @PostMapping("/getKuCunNum")
    public Integer getKuCunNum(@RequestBody FloatNumber floatNumber){
        if(floatNumber!=null){
            return iKingCanService.getKuCunNum(floatNumber);
        }else {
            return null;
        }
    }

    //保存其他出库单
    @CrossOrigin
    @PostMapping("/SaveOutChuorder")
    public ResultUtil SaveOutChuorder(@RequestBody JSONObject jsonObject) throws Exception {
        if(jsonObject.containsKey("token")){
            if(jsonObject.getString("token").length()>0) {
                String ssd = DesUtil.decrypt(jsonObject.getString("token"));
                String jsonString = iKingCanService.getSubChuParam(jsonObject);
                if(jsonString.equals("null")){
                    return ResultUtil.fail("910","物料与仓库或仓位不匹配，请核对后提交！");
                }else {
                    //InvokeHelper.Save("STK_MISCELLANEOUS", AssembleUtil.biao(), DesUtil.decrypt(jsonObject.getString("token")));
                    JSONObject jsonObject1 = InvokeHelper.Save("STK_MisDelivery", jsonString, InvokeHelper.getUserCook((jsonObject.getString("userCode"))));
                    String flag = jsonObject1.getJSONObject("Result").getJSONObject("ResponseStatus").getString("IsSuccess");
                    if (flag.equals("true")) {
                        return ResultUtil.success("");
                    } else {
                        JSONObject json = (JSONObject) JSON.toJSON(jsonObject1.getJSONObject("Result").getJSONObject("ResponseStatus").getJSONArray("Errors").get(0));
                        String msg1 = json.getString("Message");
                        return ResultUtil.fail("900", msg1);
                    }
                }
            }else {
                return ResultUtil.fail("800","会话信息已丢失，请重新登录");
            }
        }else {
            return ResultUtil.fail("700","参数为空");
        }
    }

    //根据客户id查询待审批列表
    @CrossOrigin
    @PostMapping("/idToExamBillList")
    public String idToExamBillList(@RequestBody KingCurrency kingCurrency){
        return JSONObject.toJSONString(iKingCanService.idToExamListALL(kingCurrency));

    }

    //审核工作流
    @CrossOrigin
    @PostMapping("/examWorkflow")
    public ResultUtil examWorkflow(@RequestBody KingCurrency kingCurrency) throws Exception {
            if(StringUtils.isNotBlank(kingCurrency.getToken())) {
                String jsonString = iKingCanService.getExanParm(kingCurrency);
                JSONObject jsonObject1 = InvokeHelper.WorkflowAudit(kingCurrency.getStr1(), jsonString,InvokeHelper.getUserCook((kingCurrency.getUserCode())));
                return getResult(jsonObject1);
            }else {
                return ResultUtil.fail("800","会话信息已丢失，请重新登录");
            }
    }

    //获取审批表单列表
    @CrossOrigin
    @PostMapping("/getExamType")
    public  Map<String, Object>  getExamType(@RequestBody String userCode){
       Map<String, Object> list = iKingCanService.getExanBillTypeList(userCode);
//       return JSONArray.parseArray(JSONObject.toJSONString(list));
        return list;
    }

    //提交直接调拨单
    @CrossOrigin
    @PostMapping("/submitSTkTrans")
    public ResultUtil submitSTkTrans(@RequestBody JSONObject jsonObject) throws Exception {
        if(jsonObject.containsKey("token")){
            if(jsonObject.getString("token").length()>0) {
                String jsonString = iKingCanService.getSubmitParm(jsonObject.getString("fCode"));
                JSONObject jsonObject1 = InvokeHelper.Submit("STK_TransferDirect", jsonString,InvokeHelper.getUserCook((jsonObject.getString("userCode"))));
                return getResult(jsonObject1);
            }else {
                return ResultUtil.fail("800","会话信息已丢失，请重新登录");
            }
        }else {
            return ResultUtil.fail("700","参数为空");
        }
    }

    //审核直接调拨单
    @CrossOrigin
    @PostMapping("/examSTKTrans")
    public ResultUtil examSTKTrans(@RequestBody JSONObject jsonObject) throws Exception {
        if(jsonObject.containsKey("token")){
            if(jsonObject.getString("token").length()>0) {
                String jsonString = iKingCanService.getSubmitParm(jsonObject.getString("fCode"));
                JSONObject jsonObject1 = InvokeHelper.Audit("STK_TransferDirect", jsonString,InvokeHelper.getUserCook((jsonObject.getString("userCode"))));
                return getResult(jsonObject1);
            }else {
                return ResultUtil.fail("800","会话信息已丢失，请重新登录");
            }
        }else {
            return ResultUtil.fail("700","参数为空");
        }
    }

    //反审核直接调拨单
    @CrossOrigin
    @PostMapping("/unexamSTKTrans")
    public ResultUtil unexamSTKTrans(@RequestBody JSONObject jsonObject) throws Exception {
        if(jsonObject.containsKey("token")){
            if(jsonObject.getString("token").length()>0) {
                String jsonString = iKingCanService.getSubmitParm(jsonObject.getString("fCode"));
                JSONObject jsonObject1 = InvokeHelper.UnAudit("STK_TransferDirect", jsonString, InvokeHelper.getUserCook((jsonObject.getString("userCode"))));
                return getResult(jsonObject1);
            }else {
                return ResultUtil.fail("800","会话信息已丢失，请重新登录");
            }
        }else {
            return ResultUtil.fail("700","参数为空");
        }
    }

    /**
     * 获取机构调拨业务类型列表
     *
     */
    @CrossOrigin
    @PostMapping("/getTransTypeList")
    public List<Map<String, String>> getTransTypeList(@RequestBody String fId){
        List<Map<String, String>> list = iKingCanService.getTransTypeList(fId);
        return list;
    }

    /**
     * 获取机构调拨组织机构列表
     *
     */
    @CrossOrigin
    @PostMapping("/getTransOrgan")
    public List<Map<String, String>> getTransOrgan(){
        List<Map<String, String>> list = iKingCanService.getTransOrgan();
        return list;
    }

    @CrossOrigin
    @PostMapping("/getOranCanList")
    public String getOranCanList(@RequestBody KingBdCang kingBdCang2){
//        if(redisCache.getCacheObject("Cang"+kingBdCang2.getfCode())!=null){
//          return  redisCache.getCacheObject("Cang"+kingBdCang2.getfCode());
//        }else{
//          redisCache.setCacheObject("Cang"+kingBdCang2.getfCode(),iKingBdCangService.getCangList(kingBdCang2));
          return  iKingBdCangService.getCangList(kingBdCang2);
//        }

    }

    //保存直接调拨单
    @CrossOrigin
    @PostMapping("/SaveTransorder")
    public ResultUtil SaveTransorder(@RequestBody JSONObject jsonObject) throws Exception {
        if(jsonObject.containsKey("token")){
            if(jsonObject.getString("token").length()>0) {
                String jsonString = iKingCanService.getTransParam(jsonObject);
                if(jsonString.equals("null")){
                    return ResultUtil.fail("910","物料与仓库或仓位不匹配，请核对后提交！");
                }else {
                    //InvokeHelper.Save("STK_MISCELLANEOUS", AssembleUtil.biao(), DesUtil.decrypt(jsonObject.getString("token")));
                    JSONObject jsonObject1 = InvokeHelper.Save("STK_TransferDirect", jsonString, InvokeHelper.getUserCook((jsonObject.getString("userCode"))));
                    String flag = jsonObject1.getJSONObject("Result").getJSONObject("ResponseStatus").getString("IsSuccess");
                    if (flag.equals("true")) {
                        return ResultUtil.success("");
                    } else {
                        JSONObject json = (JSONObject) JSON.toJSON(jsonObject1.getJSONObject("Result").getJSONObject("ResponseStatus").getJSONArray("Errors").get(0));
                        String msg1 = json.getString("Message");
                        return ResultUtil.fail("900", msg1);
                    }
                }
            }else {
                return ResultUtil.fail("800","会话信息已丢失，请重新登录");
            }
        }else {
            return ResultUtil.fail("700","参数为空");
        }
    }

    //根据用户内码查询直接调拨单list
    @CrossOrigin
    @PostMapping("/getUserTransList")
    public String getUserTransList(@RequestBody String fId) {
        List<Map<String, Object>> jsonArray = iKingCanService.getTransRuArry(fId);
        return JSONObject.toJSONString(jsonArray);
    }

    @CrossOrigin
    @PostMapping("/getAppVersion")
    public Map<String, String> getAppVersion(@RequestBody String fId) {
        Map<String, String> map = iKingCanService.getAppVersion(fId);
        return map;
    }

    @CrossOrigin
    @PostMapping("/getExamNum")
    public Integer getExamNum(@RequestBody String fId) {
        Integer map = iKingCanService.getExamNum(fId);
        return map;
    }

    @CrossOrigin
    @PostMapping("/delRedis")
    public void delRedis(){
        iKingCanService.delRedisKey();
    }
}
